package org.vic.observer.observer;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by VIC.Huang on 2016/8/23.
 */
public class Reader implements Observer {
    private String name;

    public Reader(String name) {
        this.name = name;
    }

    // 读者关注某个作者，关注即代表把自己加到作者的观察者列表中
    public void subscribe(String writerName) {
        WriterManager.getInstance().getWriter(writerName).addObserver(this);
        System.out.println(name + "关注了作者 " + writerName);
    }

    // 读者取消关注某个作者，取消关注即代表把自己从作者的观察者列表中移除
    public void unsubscribe(String writerName) {
        WriterManager.getInstance().getWriter(writerName).deleteObserver(this);
        System.out.println(name + "取消了对作者 " + writerName + "的关注");
    }

    // 当作者发新书时，关注的读者会收到通知
    @Override
    public void update(Observable o, Object arg) {
        if(o instanceof  Writer) {
            Writer writer = (Writer)o;
            System.out.println(name + "知道了" + writer.getName() + "发布了新书《" + writer.getLastNovel() + "》， 非要去看！");
        }
    }

    public String getName() {
        return name;
    }
}
